<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/8/23
 * Time: 19:41
 */

namespace Biz\model\activity;

use Doraemon\pockets\datebase\BaseDbModel;
use Biz\model\activity\ActivityMeal as mMeal;

class ActivityMealInfo extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_activity';

    const _DATABASE_ = 'shop_activity';

    const _TABLE_NAME = 'activity_meal_info';

    const STATUS_NORMAL = 1;
    const STATUS_END = 4;
    const STATUS_DELETE = 9;

    const STATUS_DESC = [
        self::STATUS_NORMAL => '正常',
        self::STATUS_END => '手动结束',
        self::STATUS_DELETE => '已删除',
    ];

    const STATUS_PUSH = 1;
    const STATUS_NOT_PUSH = 2;

    public static function updateMealInfo($mealInfo, $status = self::STATUS_NORMAL)
    {
        $info = self::get('*', [
            'AND' => [
                'id'=>$mealInfo['id'],
                'status' => self::STATUS_NORMAL,
            ]
        ]);

        if(mMeal::STATUS_START == $status && $info['meal_rebate']) {
            $mealInfo['meal_rebate'] = $info['meal_rebate'];
        }

        if($info) {
            $result = self::update($mealInfo, ['id'=>$mealInfo['id']]);
            $mealInfoId = $mealInfo['id'];
        } else {
            $result = self::insert($mealInfo);
            $mealInfoId = self::id();
        }

        if ($result->errorCode() !== '00000') {
            throw new \Exception('操作失败,请重试');
        }
        return $mealInfoId;
    }

    public static function delNotInMealInfo($mealId, $mealInfoIds)
    {
        $result = self::update(['status' => self::STATUS_DELETE], [
            'AND' => [
                'id[!]' => $mealInfoIds,
                'meal_id' =>$mealId
            ]

        ]);

        if($result) {
            return true;
        }

        return false;
    }

    public static function admGetMealInfoByMealId($mealId, $isFormat = true)
    {
        return self::select('*', [
            'AND' => [
                'meal_id' => $mealId,
                'status' => [self::STATUS_NORMAL, self::STATUS_END],
            ],
            'ORDER' => ['sort' => 'ASC'],
        ]);

    }

    public static function getMealInfoByMealIds($mealIds, $field = '*')
    {
        return self::select($field, [
            'AND' => [
                'meal_id' => $mealIds,
                'status' => self::STATUS_NORMAL,
            ],
            'ORDER' => ['group' => 'sort', 'sort' => 'ASC']
        ]);
    }

    public static function getMealInfoByMealIdsAndGroup($mealId, $group, $field = '*')
    {
        $time = date('Y-m-d H:i:s');
        return self::select($field, [
            'AND' => [
                'meal_id' => $mealId,
                'group' => $group,
                'status' => self::STATUS_NORMAL,
                'start_time[<=]' => $time,
                'end_time[>=]' => $time,
            ]
        ]);
    }

    public static function getMealInfoByGoodsId($goodsId, $field = '*')
    {
        $time = date('Y-m-d H:i:s');

        $mealInfo = self::select($field, [
            'AND' => [
                'goods_id' => $goodsId,
                'status' => self::STATUS_NORMAL,
                'is_push' => self::STATUS_PUSH,
                'start_time[<=]' => $time,
                'end_time[>=]' => $time,
            ],
            'ORDER' => ['sort' => 'ASC'],
        ]);
        return $mealInfo ? : [];
    }

    public static function updateStatus($mealId, $status)
    {
        $result = self::update(['status' => $status], ['meal_id' => $mealId]);

        if ($result->errorCode() !== '00000') {
            throw new \Exception('操作失败,请重试');
        }
        return $mealId;
    }
}